<template>
	<view class="main">
		<!-- <view class="item flex">
			<image src="../../static/ai.png"></image>
			<view class="item-info">
				<view>AI智能</view>
				<textarea placeholder="请说明你想要生成的文案"></textarea>
			</view>
		</view> -->
		<view class="main-tit">创作场景</view>
		<view class="navs flex align between wrap">
			<!-- <view class="nav-item" :style="navS==1?'background:#097edc':''" @click="toEdit('短视频脚本：',1)">
				<view>短视频脚本</view>
				<view>一键生成短视频脚本</view>
			</view>
			<view class="nav-item" :style="navS==2?'background:#097edc':''" @click="toEdit('直播话术：',2)">
				<view>直播话术</view>
				<view>输入要求生成直播话术</view>
			</view> -->
			<view class="nav-item" :style="navS==3?'background:#097edc':''" @click="toEdit('火爆文案：',3)">
				<view>火爆文案</view>
				<view>生成短视频文案和标题</view>
			</view>
			<navigator url="/pages/intellect/list" hover-class="none" class="nav-item">
				<view>文案库</view>
				<view>各类行业文案</view>
			</navigator>
		</view>
		<view class="main-tit">创作记录</view>
		<scroll-view scroll-y="true" :scroll-into-view="scroll_danmu_id">
			<view class="item-list">
				<view class="item flex item2" v-for="(item,index) in list" :key="index" :id="'danmu-' + index">
					<image src="../../static/ai.png"></image>
					<view class="item-info">
						<view>{{item.title}}</view>
						<view class="content" @click="toCopy(item.result)">
							<rich-text :nodes="item.result"></rich-text>
							<view class="time">{{item.addtime}}（总字数{{item.total_tokens}}）（点击文案复制）</view>
						</view>
						<!-- <textarea disabled v-model="item.result" placeholder="请说明你想要生成的文案"></textarea> -->
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="bottom flex between">
			<input placeholder="输入创作要求....." v-model="content" />
			<view @click="toSubmit">开始创作</view>
		</view>
	</view>
</template>

<script>
	import {
		aiChat,
		aiChatIndex
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				content: '',
				navS: '',
				list: [],
				scroll_danmu_id: '',
			};
		},
		onLoad() {
			this.getList()
		},
		methods: {
			toCopy(text){
				uni.setClipboardData({
					data: text,
					success: function() {
						// 可以添加用户友好的提示，例如使用uni.showToast提示复制成功
						uni.showToast({
							title: '复制成功',
							icon: 'success',
							duration: 2000
						});
					}
				});
			},
			async getList() {
				const result = await aiChatIndex()
				if (result.code == 1) {
					this.list = result.data.chats
					this.scroll_danmu_id = 'danmu-' + (this.list.length - 1);
					console.log(this.scroll_danmu_id)
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			toEdit(e, n) {
				this.navS = n
				this.content = e
			},
			async toSubmit() {
				const result = await aiChat({
					content: this.content
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg
					})
					setTimeout(()=>{
						this.content = ''
						this.getList()
					},500)
				}else{
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 0rpx 30rpx 0rpx;
		font-size: 30rpx;

		.main-tit {
			font-size: 28rpx;
			margin-top: 20rpx;
			font-weight: bold;
			color: #5D9CEB;
			padding-bottom: 20rpx;
		}

		.item-list {
			height: 758rpx;
			font-size: 28rpx;
		}

		.item {
			image {
				width: 80rpx;
				height: 80rpx;
				margin-right: 20rpx;
			}

			textarea {
				background: white;
				padding: 16rpx;
				margin-top: 9rpx;
				height: 166rpx;
				font-size: 24rpx;
				width: 100%;
			}

			.content {
				background: white;
				padding: 16rpx;
				margin-top: 9rpx;
				font-size: 22rpx;
				width: 100%;

				.time {
					color: gray;
					font-size: 18rpx;
					margin-top: 20rpx;
				}
			}

			.item-info {
				width: 80%;
			}
		}

		.navs {
			.nav-item {
				background: #2D2D2D;
				border-radius: 8rpx;
				text-align: center;
				width: 313rpx;
				padding: 14rpx 0;
				color: white;
				margin-bottom: 30rpx;
				font-size: 24rpx;

				view:nth-child(1) {
					font-weight: bold;
					font-size: 32rpx;
					margin-bottom: 6rpx;
				}
			}
		}

		.item2 {
			margin-top: 20rpx;

			image {
				margin-right: 20rpx;
			}

			.item-info {
				text-align: left;
			}

			textarea {
				width: 94%;
				margin-left: auto;
			}
		}
	}

	.bottom {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		padding: 9rpx 0;
		background: white;

		input {
			width: 500rpx;
			padding: 14rpx 20rpx;
			border-radius: 36rpx;
			font-size: 24rpx;
			margin-left: 30rpx;
			border: solid 1rpx #707070;
		}

		view {
			font-size: 24rpx;
			color: white;
			background: #333333;
			border-radius: 8rpx;
			margin-right: 30rpx;
			line-height: 71rpx;
			width: 132rpx;
			text-align: center;
		}
	}
</style>